package com.qf.controller;

import com.qf.entity.Emp;
import com.qf.service.EmpService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * --- 天道酬勤 ---
 *
 * @author GUOFUCHENG
 * @date 2025/8/8
 * @desc
 */
@RestController
public class Demo1Controller {
    @Autowired
    private EmpService empService;

    // 根据empo查询员工信息
    @GetMapping("/emp/{empo}")
    public R findByEmpo(@PathVariable int empo) {
        System.out.println("empo = " + empo);
        Emp emp = empService.findByEmpo(empo);
        System.out.println("emp = " + emp);
        if (emp == null) {
            return R.fail(404, "emp not found");
        }
        return R.success(emp);
    }

    //查询全部员工信息(集合来装载)
    @GetMapping("/emp")
    public R findAll(Emp emp) {
        System.out.println("emp = " + emp);
        List<Emp> emps = empService.findAll(emp);
        // System.out.println("emps = " + emps);
        if (emps == null || emps.size() == 0) {
            return R.fail(404, "emp not found");
        } else {
            //循环遍历集合，将集合中的对象转换为json字符串
            for (Emp e : emps) {
                System.out.println(e);
            }
            return R.success(emps);
        }
    }

    //通过员工姓名 员工编号查询员工信息
    @GetMapping("/emp/name/{ename}/empno/{empno}")
    public R findByNameAndEmpno(@PathVariable String ename, @PathVariable int empno) {
        Emp emp = empService.findByNameAndEmpno(ename, empno);
        System.out.println("emp = " + emp);
        if (emp == null) {
            return R.fail(404, "emp not found");
        }
        return R.success(emp);
    }


    //多参数查询(根据用户名 以及聘用时间 模糊查询员工信息 )
    @GetMapping("/emp/search")
    public R findMap(@RequestParam Map<String, String> map) {
        System.out.println("map = " + map);
        List<Emp> emps = empService.findMap(map);
        System.out.println("emps = " + emps);
        if (emps == null || emps.size() == 0) {
            return R.fail(404, "emp not found");
        } else {
            return R.success(emps);
        }
    }

    //添加员工信息

    @PostMapping("/emp/add")
    public R addEmp(Emp emp) {
        System.out.println("emp = " + emp);
        int result = empService.addEmp(emp);
        if (result == 1) {
            return R.success("添加成功");
        } else {
            return R.fail(404, "添加失败");
        }
    }


    //修改员工信息 先根据empno查询员工信息，然后修改员工信息，最后更新员工信息
    @PostMapping("/emp/update")
    public R updateEmp(Emp emp) {
        System.out.println("emp = " + emp);
        Emp emp1 = empService.findByEmpo(emp.getEmpno());
        System.out.println("emp1 = " + emp1);
        if (emp1 == null) {
            return R.fail(404, "emp not found");
        }
        emp1.setEname(emp.getEname());
        emp1.setJob(emp.getJob());
        emp1.setMgr(emp.getMgr());
        emp1.setHiredate(emp.getHiredate());
        emp1.setSal(emp.getSal());
        emp1.setComm(emp.getComm());
        emp1.setDeptno(emp.getDeptno());

        int result = empService.updateEmp(emp1);
        if (result == 1) {
            return R.success("修改成功");
        } else {
            return R.fail(404, "修改失败");
        }
    }



    //删除员工信息(通过员工号empno删除员工信息)
    @PostMapping("/emp/delete/{empno}")
    public R deleteEmp(@PathVariable int empno){
        System.out.println("empno = " + empno);
       int result = empService.deleteEmp(empno);
        System.out.println("result = " + result);
        if (result == 0) {
            return R.fail(404, "删除失败");
        }
        return R.success("删除成功");
    }



}
